package dxn.nl.file.api.export;

import com.alibaba.fastjson2.JSONObject;
import dxn.nl.file.client.core.CsExportDealServer;
import dxn.nl.file.client.core.DealExportService;
import dxn.nl.file.client.model.dto.*;
import dxn.nl.file.client.model.enums.ExportDealResultTypeEnum;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@CsExportDealServer(value = {"demo_export_page_muti_sheet"})
public class DealPageSheetExport implements DealExportService {
    @Override
    public CsExportDealTypeHeadDto getExcelHeads(TransferMessageConfig config, TransferMessageData messageData, byte[] data) {
        List<CsExportExcelHeadDto> dtoList = Arrays.asList(new CsExportExcelHeadDto("code","编号",1,30),
                new CsExportExcelHeadDto("name","名称",2,15),
                new CsExportExcelHeadDto("age","年龄",3,10),
                new CsExportExcelHeadDto("address","地址",20));
        CsExportDealTypeHeadDto dto = CsExportDealTypeHeadDto.pageDeal(dtoList);

        List<CsExportExcelHeadDto> sheet2List = Arrays.asList(new CsExportExcelHeadDto("code","sheet2编号",1,30),
                new CsExportExcelHeadDto("name","sheet2名称",2,15),
                new CsExportExcelHeadDto("age","sheet2年龄",3,10),
                new CsExportExcelHeadDto("address","sheet2地址",20));

        CsExportExcelSheetHeadDto sheetHeadDto = new CsExportExcelSheetHeadDto();
        sheetHeadDto.setHeadDtoList(sheet2List);
        sheetHeadDto.setHasTitle(true);

        dto.setSheetHeads(sheetHeadDto,"测试sheet2");
        return dto;
    }

    @Override
    public  CsExportDealResult queryData(TransferMessageConfig config, TransferMessageData messageData, byte[] data) {
        CsExportDealResult dealResult = new CsExportDealResult();
        dealResult.setDealResult(ExportDealResultTypeEnum.continue_deal);

        if(messageData.getCurrentParam()!=null) {
            dealResult.setDealResult(ExportDealResultTypeEnum.end);
            return dealResult;
        }

        dealResult.setCurrentParam("ceshi");

        if("测试sheet2".equalsIgnoreCase(messageData.getSheetName()))
        {
            List<JSONObject> list = new ArrayList<>();
            for(int i=0;i<10;i++)
            {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", "code"+i);
                jsonObject.put("name", "name"+i);
                jsonObject.put("age", "age"+i);
                jsonObject.put("address", "address"+i);
                list.add(jsonObject);
            }
            dealResult.setData(list);
        }
        else {
            List<ItemPo> list = new ArrayList<>();
            for(int i=0;i<10;i++)
            {
                list.add(new ItemPo( "code"+i,"name"+i,"age"+i,"address"+i));
            }
            dealResult.setData(list);
        }

        return dealResult;
    }

    @Data
    class ItemPo {
        private String code;
        private String name;
        private String age;
        private String address;

        public ItemPo(String code, String name, String age,String address)
        {
            this.code = code;
            this.name = name;
            this.age = age;
            this.address = address;
        }
    }
}
